package com.qf.shop.controller;

import com.qf.shop.entity.Goods;
import com.qf.shop.entity.Page;
import com.qf.shop.service.GoodsService;
import com.qf.shop.service.impl.GoodsServiceImpl;
import com.qf.shop.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/goods")
public class GoodsController extends BaseController {
    //分页展示所有商品
    public String showAll(HttpServletRequest request, HttpServletResponse response) {
        //1.收参
        String typeid = request.getParameter("typeid");
        String pageIndex = request.getParameter("pageIndex");
        if(pageIndex==null){
            pageIndex="1";
        }
        Page page =new Page(Integer.valueOf(pageIndex));
        //2.根据id调用业务  查询到集合  分页
        GoodsService goodsService =new GoodsServiceImpl();
        List<Goods> glist= goodsService.getAllGoodsById(page, Integer.valueOf(typeid));

        //3将分页对象。集合存储到作用域
        request.setAttribute("page", page);
        request.setAttribute("glist",glist);
        return Constants.FORWARD+"/goodsList.jsp";
    }
    //展示单个商品
    public String show(HttpServletRequest request,HttpServletResponse response){
        //收参    获取商品的id
        String id = request.getParameter("id");
        //2调用 service ,查询单个商品
        GoodsService goodsService = new GoodsServiceImpl();
        Goods goods = goodsService.getGoodById(Integer.valueOf(id));
        //3.将商品存储在作用域中，转发到goodsDetail.jsp
        request.setAttribute("goods",goods);
        return Constants.FORWARD+"/goodsDetail.jsp";

    }

}
